package com.yunchang.springboot.scheduled.amazonAdvert;

import com.yunchang.springboot.mapper.amazonAdvert.ErrorDateMapper;
import com.yunchang.springboot.mapper.shop.AreaMapper;
import com.yunchang.springboot.mapper.shop.ProfileMapper;
import com.yunchang.springboot.mapper.shop.ShopMapper;
import com.yunchang.springboot.models.Area;
import com.yunchang.springboot.models.DO.ErrorDateDO;
import com.yunchang.springboot.models.DO.ShopAreaProfile;
import com.yunchang.springboot.models.Shop;
import com.yunchang.springboot.service.AmazonAdvert.AmazonAdvertBrandsReportDataProcessingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
@Component
public class AmazonAdvBrandsDataSchedule {
    @Autowired
    AmazonAdvertBrandsReportDataProcessingService brandsReportDataProcessingService;
    @Autowired
    ProfileMapper profileMapper;
    @Autowired
    ErrorDateMapper errorDateMapper;
    @Autowired
    ShopMapper shopMapper;
    @Autowired
    AreaMapper areaMapper;

    //@Scheduled(cron = "0 0 20 ? * *")
    public void addLastMonthAdvBrandsReport() {
        //当前时间
        log.info("获取前一个月品牌相关数据的定时任务开始");
        LocalDateTime localDateTime = LocalDateTime.now();
        String date;
        //晚上8点  定时任务去拿昨天的数据
        List<ShopAreaProfile> shopAreaProfileList = profileMapper.getAllDatas();
        for (ShopAreaProfile shopAreaProfile : shopAreaProfileList) {
            for (int i = 45; i <= 60; i++) {
                date = localDateTime.minusDays((long) i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                brandsReportDataProcessingService.addOriginalDataAdvBrandsCampaignsReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsCampaignsReport添加" + (1) + "次");
                brandsReportDataProcessingService.addOriginalDataAdvBrandsAdGroupsReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsAdGroupsReport添加" + (1) + "次");
                brandsReportDataProcessingService.addOriginalDataAdvBrandsKeywordsReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsKeywordsReport添加" + (1) + "次");
                brandsReportDataProcessingService.addOriginalDataAdvBrandsTargetsReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsTargetsReport添加" + (1) + "次");
                brandsReportDataProcessingService.addOriginalDataAdvBrandsSearchTermReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsSearchTermReport添加" + (1) + "次");
                log.info("-----------------------店铺id：" + shopAreaProfile.getShopId() + "和区域id:" + shopAreaProfile.getAreaId() + "和配置文件id:" + shopAreaProfile.getProfileId() + "获取" + date + "的品牌相关报表数据任务完成-------------------------------");
            }
        }
        log.info("-----------------------目前所有区域获取前一个月:" + localDateTime.toString() + "的品牌相关报表的数据任务完成-------------------------------");
    }

    //@Scheduled(cron = "00 00 23 ? * *")
    public void addYesterdayAdvBrandsReport() {
        //当前时间
        log.info("获取昨天品牌相关数据的定时任务开始");
        LocalDateTime localDateTime = LocalDateTime.now();
        String date;
        //晚上8点  定时任务去拿昨天的数据
        List<ShopAreaProfile> shopAreaProfileList = profileMapper.getAllDatas();
        for (ShopAreaProfile shopAreaProfile : shopAreaProfileList) {
            date = localDateTime.minusDays(1L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            brandsReportDataProcessingService.addOriginalDataAdvBrandsCampaignsReport(date, shopAreaProfile);
            System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsCampaignsReport添加" + (1) + "次");
            brandsReportDataProcessingService.addOriginalDataAdvBrandsAdGroupsReport(date, shopAreaProfile);
            System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsAdGroupsReport添加" + (1) + "次");
            brandsReportDataProcessingService.addOriginalDataAdvBrandsKeywordsReport(date, shopAreaProfile);
            System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsKeywordsReport添加" + (1) + "次");
            brandsReportDataProcessingService.addOriginalDataAdvBrandsTargetsReport(date, shopAreaProfile);
            System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsTargetsReport添加" + (1) + "次");
            brandsReportDataProcessingService.addOriginalDataAdvBrandsSearchTermReport(date, shopAreaProfile);
            System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsSearchTermReport添加" + (1) + "次");
            log.info("-----------------------店铺id：" + shopAreaProfile.getShopId() + "和区域id:" + shopAreaProfile.getAreaId() + "和配置文件id:" + shopAreaProfile.getProfileId() + "获取" + date + "的品牌相关报表数据任务完成-------------------------------");
        }
        log.info("-----------------------目前所有区域获取昨天:" + localDateTime.toString() + "的广告数据任务完成-------------------------------");
    }

   // @Scheduled(cron = "00 00 00 ? * *")
    @Transactional(rollbackFor = Exception.class)
    public void reAddAdvertErrorDateData() {
        log.info("Current Thread : {}", Thread.currentThread().getName());
        log.info("-----------------------获取之前错误日期的数据任务开始-------------------------------");
        List<ErrorDateDO> errorDateDOList = errorDateMapper.selectErrorDateData();
        for (ErrorDateDO errorDateDO : errorDateDOList) {
            String sellerId = errorDateDO.getSellerId();
            String areaCode = errorDateDO.getArea();
            Shop shop = shopMapper.getOneInfoBySellerId(sellerId);
            Area area = areaMapper.getAreaByAdvertCountryCode(areaCode);
            ShopAreaProfile shopAreaProfile = profileMapper.getDatasByShopIdAreaId(shop.getId(), area.getId());
            Integer type = errorDateDO.getType();
            String date = errorDateDO.getErrorDate();
//            int status = errorDateDO.getStatus();
//            if (status == 2){
//                continue;
//            }
            if (type == 7) {
                errorDateMapper.delErrorDate(errorDateDO);
                brandsReportDataProcessingService.addOriginalDataAdvBrandsCampaignsReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsCampaignsReport添加" + (1) + "次");
                continue;
            }
            if (type == 8) {
                errorDateMapper.delErrorDate(errorDateDO);
                brandsReportDataProcessingService.addOriginalDataAdvBrandsAdGroupsReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsAdGroupsReport添加" + (1) + "次");
                continue;
            }
            if (type == 9) {
                errorDateMapper.delErrorDate(errorDateDO);
                brandsReportDataProcessingService.addOriginalDataAdvBrandsKeywordsReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsKeywordsReport添加" + (1) + "次");
                continue;
            }
            if (type == 10) {
                errorDateMapper.delErrorDate(errorDateDO);
                brandsReportDataProcessingService.addOriginalDataAdvBrandsTargetsReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsTargetsReport添加" + (1) + "次");
                continue;
            }
            if (type == 11) {
                errorDateMapper.delErrorDate(errorDateDO);
                brandsReportDataProcessingService.addOriginalDataAdvBrandsSearchTermReport(date, shopAreaProfile);
                System.out.println("店铺id：" + shopAreaProfile.getShopId() + "的BrandsSearchTermReport添加" + (1) + "次");
                continue;
            }
        }
        log.info("-----------------------获取之前错误日期的数据任务完成-------------------------------");
    }
}
